var mw = 320, mh = 240, mdl = md.length - 1, cpc = mw * mh * 4, fr = -1;

// helpers
function $ (id) { return document.getElementById(id); }
function pi (i) { var o = parseInt(i, 10); return o === NaN ? 9 : o; }
function mr (i) { return Math.round(i); }
function ua (d) {
  var out = d;
  out = out.replace(/(.)\*([0-9]+)\!/g, function (m, s, c) { var o = []; for (var i = 0; i < c; i++) o.push(s); return o.join(''); });
  for (var i in ht) {
    if (ht.hasOwnProperty(i)) {
      out = out.replace(ht[i], i);
    }
  }
  return out;
}

// main-loop
function pf (c) {
  setTimeout(function () { pf(c) }, 1);
  var cf = mr((new Date() - st) / 24);
  if (!(cf > fr && cf < mdl)) return;
  var fd = ua(md[cf]),
      ids = c.createImageData ? c.createImageData(mw, mh) : c.getImageData(0, 0, mw, mh);
  for (var i = 0; i < cpc; i += 4) {
    ids.data[i] = ids.data[i + 1] = ids.data[i + 2] = 25 * (pi(fd[i / 4]) + 1);
    ids.data[i + 3] = 255;
  }
  c.putImageData(ids, 0, 0);
  fr = cf;
}

// init
window.onload = function () {
  var scr = $('s'), scrs = scr.style;
  scrs.width = mw + 'px'; scrs.height = mh + 'px';
  st = new Date();
  var c = scr.getContext ? scr.getContext("2d") : null;
  if (!c) {
    scrs.display = 'none';
    $('old').style.display = 'block';
  }
  setTimeout(function () { pf(c); }, 1);
};
